#回合制游戏：两人进入到一个房间，相互攻击
#创建Game类：游戏
#   交互式菜单：创建角色，创建房间，添加角色到房间，房间满员自动开始游戏
#创建Room类：房间
#   房间名，房间编号，房间成员，房间最大人数
#   加入房间：房间满员自动开始游戏(player1,player2)
#   room.add(player)
#创建Role类：角色
#   初始血量100、初始积分100
#   名字、血量、积分、当前所在房间
#   攻击 a.attack(b)随机掉血（10-30）
#   role.join(room)
class Game:
    flag = 0
    def __init__(self):
        self.player = {}
        name = input("请输入要创建的角色名：")
        self.player[name] = Role(name, 100, 30)
        self.player[name].show()
        room_name = input("请输入要创建的房间名：")
        room_number = int(input("请输入要房间人数："))
        self.room = Room(room_name, 0, room_number)
        self.room.add(name)
        self.room.show()

    def start(self):
        choice = input("1.继续创建角色\n2.邀请角色加入房间\n请输入：")
        if choice == "1":
            name = input("请输入要创建的角色名：")
            self.player[name] = Role(name, 100, 30)
            self.player[name].show()
        if choice == "2":
            player_name = input("请输入被邀请人名字：")
            self.room.add(player_name)

    def attack(self):
        for i in self.player:
            if not self.player[i].flag2:
                print(f"{i}发动攻击！")
                name = input("请输入要攻击玩家的名字:")
                self.player[i].attack_obj(self.player[name])
                self.player[name].show()
            else:
                print(f"游戏结束！")
                self.flag = 1
                for j in self.player:
                    self.player[j].show()

class Room:
    room_member = []
    flag = 0
    def __init__(self, room_name, number, max_person):
        self.room_name = room_name
        self.number = number
        self.max_person = max_person

    def add(self, player):
        self.room_member.append(player)
        self.show()
        if len(self.room_member) == self.max_person:
            print("房间人数已满，游戏开始")
            self.flag = 1

    def show(self):
        print(f"房间名：{self.room_name}，房间号：{self.number}，房间最大人数是：{self.max_person}")
        print("房间成员有：", self.room_member)

import random
class Role:
    INIT_HP = 100
    score = 100
    flag2 = 0
    def __init__(self, name, hp = None, attack = 50):
        self.name = name
        if hp:
            self.hp = hp
        else:
            #self.hp = self.INIT_HP
            self.hp = self.__class__.INIT_HP
        #攻击上限
        self.attack = attack

    def show(self):
        print(f"{self.name}当前的血量为:{self.hp},当前的积分为：{self.score}")

    def attack_obj(self, att_obj):
        if self.hp:
            x = random.randint(1, self.attack)
            print(f"{self.name}对{att_obj.name}造成了{x}点伤害")
            if att_obj.hp < x:
                print(f"{att_obj.name}玩家死亡")
                att_obj.hp = 0
                att_obj.flag2 = 1
                self.score += 1
            else:
                att_obj.hp -= x

game = Game()
while not game.room.flag:
    game.start()
while not game.flag:
    game.attack()

